package org.com.lcuiot.service;

import org.com.lcuiot.database.entity.User;
import org.com.lcuiot.database.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import static org.springframework.web.reactive.function.server.ServerResponse.ok;

import java.util.Optional;

/**
 * @author gaoge
 * @since 2022/10/23 22:08
 */
@Service
public class UserExampleService {
    private final UserMapper userMapper;

    @Autowired
    public UserExampleService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public Optional<User> addUser(String name) {
        User user = new User();
        user.setUsername(name);
        userMapper.insert(user);
        Optional<User> uu = userMapper.selectByPrimaryKey(user.getId());
        return uu;
    }

    public Optional<User> deleteUser(Long id) {
        Optional<User> uu = userMapper.selectByPrimaryKey(id);
        if(uu.isPresent()) {
            userMapper.delete(uu.get());
        }
        return uu;
    }

    public Optional<User> patchUser(Long id, String name) {
        Optional<User> uu = userMapper.selectByPrimaryKey(id);
        if(uu.isPresent()) {
            User u = uu.get();
            u.setUsername(name);
            userMapper.updateByPrimaryKey(u);
        }
        return uu;
    }

}
